package com.warmguard.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.warmguard.core.domain.entity.Images;
import com.warmguard.core.domain.vo.ProductDetailsVO;
import com.warmguard.core.service.ImagesService;
import com.warmguard.core.service.ProductService;
import com.warmguard.core.domain.entity.Product;
import com.warmguard.core.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 32782
 * @description 针对表【product(产品表)】的数据库操作Service实现
 * @createDate 2025-01-21 21:59:11
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
        implements ProductService {

    @Autowired
    private ImagesService imageService;

    @Override
    public Page<Product> search(Page<Product> productPage, String keyWord) {
        if (keyWord != null && !keyWord.equals("")) {
            return this.page(productPage, null);
        }
        LambdaQueryWrapper<Product> w = new LambdaQueryWrapper<>();
        w.like(Product::getProductName, keyWord).or(wrapper -> wrapper.like(Product::getDescription, keyWord));
        return this.page(productPage, w);
    }

    @Override
    public ProductDetailsVO getProductWithDetailsById(Long id) {
        Product byId = this.getById(id);
        ProductDetailsVO productDetailsVO = new ProductDetailsVO();
        BeanUtils.copyProperties(byId, productDetailsVO);
        LambdaQueryWrapper<Images> w = new LambdaQueryWrapper<>();
        w.eq(Images::getGoodsId, id);
        List<Images> list = imageService.list(w);
        productDetailsVO.setImages(list);
        return productDetailsVO;
    }
}




